package ui
{
	import assets.Asset;
	
	import events.P2pEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.Responder;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	
	import game.Citadel;
	import game.StoreHouse;
	
	import gameObject.CitadelMap;
	import gameObject.User;
	
	import net.P2p;
	
	import tool.BitmapTool;
	
	import ui.component.Button;
	import ui.component.TextInput;

	
	public class LoginWindow extends Sprite
	{
		private var userNameInput:TextInput;
		private var passwordInput:TextInput;
		
		private var registNameInput:TextInput;
		private var registPwInput:TextInput;
		private var registPwReInput:TextInput;
		
		private var userName:String;
		private var passWord:String;
		
		private var xmlLoader:URLLoader;
		private var xml:XML;
		
		private var bmdLoader:Loader;
		private var bmds:Dictionary;
		
		private var btnlogin:Button;
		private var btnRegist:Button;
		
		public function LoginWindow()
		{
			xmlLoader = new URLLoader;
			
			xmlLoader.addEventListener(Event.COMPLETE,xmlComplet);
			xmlLoader.load(new URLRequest(Global.assetPath+"UI/login.xml"));
		}
		
		private function xmlComplet(e:Event):void
		{
			xmlLoader.removeEventListener(Event.COMPLETE,xmlComplet);
			
			xml =new XML(xmlLoader.data);
			
			this.x = int(xml.attribute("posX"));
			this.y = int(xml.attribute("posY"));
			
			bmdLoader = new Loader;
			bmdLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,bmdLoaded);
			bmdLoader.load(new URLRequest(Global.assetPath+String(xml.attribute("ima"))));
		}
		
		private function bmdLoaded(e:Event):void
		{
			bmdLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE,bmdLoaded);
			var bmdSource:BitmapData = ((bmdLoader.content) as Bitmap).bitmapData.clone();
			bmdLoader.unload();
			
			bmds = BitmapTool.cutImageUseXML(bmdSource,xml);
			bmdSource.dispose();
			
			buildUI();
		}
		
		/**
		 * 构建UI
		 */		
		private function buildUI():void
		{
			var background:Bitmap = new Bitmap(bmds["background"]);
			background.x = int(xml.background.attribute("posX"));
			background.y = int(xml.background.attribute("posY"));
			
			btnlogin = new Button(bmds["btnlogin"],bmds["btnlogin"]);
			btnlogin.x = int(xml.btnlogin.attribute("posX")); 
			btnlogin.y = int(xml.btnlogin.attribute("posY")); 
			
			btnRegist = new Button(bmds["btnregist"],bmds["btnregist"]);
			btnRegist.x = int(xml.btnregist.attribute("posX")); 
			btnRegist.y = int(xml.btnregist.attribute("posY")); 
			
			userNameInput = new TextInput(16,138,24,12);
			userNameInput.x = 71;
			userNameInput.y = 64;
			
			passwordInput = new TextInput(18,138,18,12,true);
			passwordInput.x = 71;
			passwordInput.y = 117;
			
			registNameInput = new TextInput(16,138,24,12);
			registNameInput.x = 311;
			registNameInput.y = 47;
			
			registPwInput = new TextInput(18,138,18,12,true);
			registPwInput.x = 311;
			registPwInput.y = 93;
			
			registPwReInput = new TextInput(18,138,18,12,true);
			registPwReInput.x = 311;
			registPwReInput.y = 124;
			
			addChild(background);
			addChild(userNameInput);
			addChild(passwordInput);
			addChild(registNameInput);
			addChild(registPwInput);
			addChild(registPwReInput);
			addChild(btnlogin);
			addChild(btnRegist);
			
			btnlogin.addEventListener(MouseEvent.CLICK,callLogin);
			btnRegist.addEventListener(MouseEvent.CLICK,callRegist);
		}
		//----------------------------------------------------------
		
		/**
		 * 发送登陆申请
		 */		
		private function callLogin(e:MouseEvent):void
		{
			userName = userNameInput.text
			passWord = passwordInput.text
			if(userName != "")
			{
				Global.phpServer.call("userServer.login",new Responder(login),userName,passWord);
			}
		}
		
		/**
		 * 服务器反馈登陆信息后的处理
		 */		
		private function login(obj:Object):void
		{
			trace(1);
			if(obj>0)//登陆成功并从服务端取得用户数据
			{
				btnlogin.removeEventListener(MouseEvent.CLICK,callLogin);
				btnRegist.removeEventListener(MouseEvent.CLICK,callRegist);
				
				for each(var bmd:BitmapData in bmds)bmd.dispose();
				bmds = null;
				
				Global.uid = uint(obj);
				Global.stage.removeChild(this);
				
				Global.user = new User(Global.uid);
				Global.user.addEventListener(Event.COMPLETE,gotUserData);
				
				
				Global.P2PServer = new P2p(Global.p2pAddress);
				Global.P2PServer.addEventListener(P2pEvent.CONNECTED_TO_SERVER,onP2PConnected);
				
			}else trace("用户名不存在或密码不正确");
		}
		
		/**
		 * 发送注册申请
		 */		
		private function callRegist(e:MouseEvent):void
		{
			userName = registNameInput.text;
			passWord = registPwInput.text;
			var rePassWord:String = registPwReInput.text;
			
			if(userName != "")
			{
				if(passWord == rePassWord)
				{
					Global.phpServer.call("userServer.regist", new Responder(registed),userName,passWord);
				}else trace("两次输入的密码不符");
				
			}else trace("用户名不能为空");
		}
		
		/**
		 * 反馈服务器发回的注册申请处理
		 */		
		private function registed(obj:Object):void
		{
			if(uint(obj)>0)
			{
				trace("注册成功");
				Global.phpServer.call("User.login", new Responder(login),userName,passWord);
			}
			else
			{
				trace("用户名已存在");
			}
		}
		
		/**
		 * 收到用户数据后进入下一幕
		 */
		private function gotUserData(e:Event):void
		{
			Global.user.removeEventListener(Event.COMPLETE,gotUserData);
			Global.stage.addChild(new Citadel);
		}
		
		private function onP2PConnected(e:P2pEvent):void
		{
			//trace("已连接上P2P服务器");
		}
	}
}